package com.artpri.action.user;

import java.io.PrintWriter;

import com.artpri.action.BaseAction;
import com.artpri.model.UserInfo;
import com.google.gson.Gson;

/**
 * 用户注册
 * @author ZhangSi
 */
public class RegisterAction extends BaseAction{

	private static final long serialVersionUID = -1826287830909016675L;

	private UserInfo user;
	
	private String redirect;
	
	private String name;
	
	private String secCode;
	
	/**
	 *	邮箱注册 
	 */
	public String emailRegist() throws Exception {
		
		String activateCode = getUserService().emailRegist(user);
		
		getEmailService().htmlSend(user.getUserEmail(), "http://localhost:8080/jpw/reg/activate?id=" + user.getUserNum() + "&data=" + activateCode, user.getUserName());
		
		return SUCCESS;
	}
	
	/**
	 *  手机注册
	 */
	public String mobileRegist() throws Exception {
		// ╮(╯▽╰)╭  等等在写吧。。。
		return null;
	}

	/**
	 * 检查用户名是否已存在
	 */
	public String checkUserExists() throws Exception {
		boolean b = getUserService().checkUserNameExists(name);
		
		PrintWriter out = getWriter();
		
		System.out.println(new Gson().toJson(b));
		
		String result = "notexists";
		
		if (b) {
			result = "exists";
		}
		
		out.print(new Gson().toJson(result));
		
		out.flush();
		out.close();
		
		return null;
	}
	
	/**
	 * 检查验证码输入是否正确
	 */
	public String checkSecurityCodeRight() throws Exception {
		String code = (String) getSession().get("securityCode");
		
		boolean b = secCode.equals(code);
		
		PrintWriter out = getWriter();
		
		out.print(b);
		
		out.flush();
		out.close();
		
		return null;
	}
	
	public UserInfo getUser() {
		return user;
	}

	public void setUser(UserInfo user) {
		this.user = user;
	}

	public String getRedirect() {
		return redirect;
	}

	public void setRedirect(String redirect) {
		this.redirect = redirect;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSecCode() {
		return secCode;
	}

	public void setSecCode(String secCode) {
		this.secCode = secCode;
	}
	
}
